<?php

namespace App\Admin\Controllers;

use App\Admin\Grids\MsgList;
use App\Admin\Grids\PhoneList;
use App\Models\Customer;
use App\Models\Msg;
use App\Models\Phone;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CustomerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Customer(), function (Grid $grid) {
            $grid->column('id','用户编号')->sortable();
            $grid->column('name');
            $grid->column('phone');
            $grid->column('code');
            $grid->column('notice')->editable();;
            $grid->column('通讯录')
                ->display('通讯录') // 设置按钮名称
                ->modal(function ($modal) {
                    // 设置弹窗标题
                    $modal->title($this->name."[".$this->id."]的通讯录");
                    $modal->xl();
                    // 自定义图标
                    $modal->icon('feather icon-eye');
                    $grid = PhoneList::make()->payload(['uid' => $this->id]);
                    return $grid;
                });
            $grid->column('短信记录')
                ->display('短信记录') // 设置按钮名称
                ->modal(function ($modal) {
                    // 设置弹窗标题
                    $modal->title($this->name."[".$this->id."]的短信列表");
                    $modal->xl();
                    // 自定义图标
                    $modal->icon('feather icon-eye');
                    $grid = MsgList::make()->payload(['uid' => $this->id]);
                    return $grid;
                });

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id','用户编号');
            });
            $grid->export();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Customer(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('code');
            $show->field('notice');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Customer(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('phone');
            $form->text('code');
            $form->text('notice');
            $form->display('created_at');
            $form->display('updated_at');
            $form->deleted(function (Form $form){
                try {
                    $data = $form->model()->toArray();
                    foreach ($data as $index => $value){
                        Phone::where('pid',$value['id'])->delete();
                        Msg::where('pid',$value['id'])->delete();
                    }
                    return $form->response()->success('删除成功');
                } catch (\Exception $e) {
                    return $form->response()->error('数据删除失败');
                }
            });
        });
    }
}
